package com.offcn.seckill.config;

import javafx.scene.layout.Background;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 队列以及绑定对应交换机
 *
 * @author 秋林
 * @date 2022/10/21 17:09:53
 */

@Component
public class bind {

    @Autowired
    private Environment environment;

    /**
     * 创建DirectExchange交换机
     *
     * @return {@link DirectExchange}
     */
    public DirectExchange baseDirectExchange() {
        return new DirectExchange(environment.getProperty("mq.pay.exchange.order"), true, false);
    }

    /**
     * 创建队列
     * @return {@link Queue}
     */
    public Queue queueOrder() {
        return new Queue(environment.getProperty("mq.pay.exchange.order"),true);
    }

    /**
     * 创建秒杀队列
     *
     * @return {@link Queue}
     */
    @Bean(name = "queueSeckillOrder")
    public Queue queueSeckillOrder(){
        return new Queue(environment.getProperty("mq.pay.queue.seckillorder"),true);
    }

    /**
     * 队列绑定到交换机上
     *
     * @return {@link Binding}
     */
    public Binding basicBindingOrder(){
        return BindingBuilder.bind(queueOrder())
                .to(baseDirectExchange())
                .with(environment.getProperty("mq.pay.routing.orderkey"));
    }

    /**
     * 队列绑定到交换机上
     * @return
     */
    public Binding basicBindingSeckillOrder(){
        return BindingBuilder
                .bind(queueSeckillOrder())
                .to(baseDirectExchange())
                .with(environment.getProperty("mq.pay.routing.seckillorderkey"));
    }
}
